﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommonHelper
{
    /// <summary>
    /// 排列组合
    /// </summary>
    public static class Permutation
    {

        /// <summary>
        /// 返回字符串的所有组合方式
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        public static List<string> GetEveryUniquePermutation(string word)
        {
            if (word.Length < 2)
            {
                return new List<string>
                {
                    word,
                };
            }

            var result = new HashSet<string>();

            for (var i = 0; i < word.Length; i++)
            {
                var temp = GetEveryUniquePermutation(word.Remove(i, 1));

                result.UnionWith(temp.Select(subPerm => word[i] + subPerm));
            }

            return result.ToList();
        }

    }
}
